(function (Spine, $) {

    var Model = Spine.Model;

    var methodMap = {
        "create": "POST",
        "update": "PUT",
        "destroy": "DELETE",
        "read": "GET"
    };

    var urlError = function () {
        throw new Error("A 'url' property or function must be specified");
    };

    var ajaxSync2 = function (method, model) {
        if (Model._noSync) return;

        var params = {
            type: methodMap[method],
            contentType: "application/json",
            dataType: "json",
            processData: false
        };

        params.url = model.url;

        if (!params.url) throw ("Invalid URL");

        if (method == "read")
            params.success = function (data) {
                (model.refresh || model.load).call(model, data);
                model.trigger("fetched", model);
            };

        params.error = function (xhr, s, e) {
            model.trigger("ajaxError", xhr, s, e);
        };

        $.ajax(params);
    };


    Model.include({
        url: "",
        fetch: function () {
            ajaxSync2("read", this);
        }
    });


    Model.Ajax2 = {
        extended: function () {

        }
        //                this.sync(ajaxSync);
        //                    this.fetch(this.proxy(function(){
        //                      ajaxSync("read", this);
        //                    }));            
    };


    //Model.extend({
    //  ajaxPrefix: false,
    //  
    //  url: function() {
    //    return "/" + this.name.toLowerCase() + "s"
    //  },
    //  
    //  noSync: function(callback){
    //    Model._noSync = true;
    //    callback.apply(callback, arguments);
    //    Model._noSync = false;
    //  }
    //});

    //Model.include({
    //  url: function(){
    //    var base = getUrl(this.parent);
    //    base += (base.charAt(base.length - 1) == "/" ? "" : "/");
    //    base += encodeURIComponent(this.id);
    //    return base;        
    //  }  
    //});

})(Spine, Spine.$);